﻿namespace UnityEditor
{
    using System;
    using System.Runtime.CompilerServices;
    using UnityEditorInternal;
    using UnityEngine;

    public sealed class BuildPipeline
    {
        public static bool BuildAssetBundle(UnityEngine.Object mainAsset, UnityEngine.Object[] assets, string pathName)
        {
            BuildTarget webPlayer = BuildTarget.WebPlayer;
            BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies;
            return BuildAssetBundle(mainAsset, assets, pathName, assetBundleOptions, webPlayer);
        }

        public static bool BuildAssetBundle(UnityEngine.Object mainAsset, UnityEngine.Object[] assets, string pathName, BuildAssetBundleOptions assetBundleOptions)
        {
            BuildTarget webPlayer = BuildTarget.WebPlayer;
            return BuildAssetBundle(mainAsset, assets, pathName, assetBundleOptions, webPlayer);
        }

        public static bool BuildAssetBundle(UnityEngine.Object mainAsset, UnityEngine.Object[] assets, string pathName, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform)
        {
            try
            {
                return BuildAssetBundleInternal(mainAsset, assets, null, pathName, assetBundleOptions, targetPlatform);
            }
            catch (System.Exception)
            {
                Debug.LogError("Internal Error in BuildPlayer.");
                EditorApplication.Exit(1);
                return false;
            }
        }

        public static bool BuildAssetBundleExplicitAssetNames(UnityEngine.Object[] assets, string[] assetNames, string pathName)
        {
            BuildTarget webPlayer = BuildTarget.WebPlayer;
            BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies;
            return BuildAssetBundleExplicitAssetNames(assets, assetNames, pathName, assetBundleOptions, webPlayer);
        }

        public static bool BuildAssetBundleExplicitAssetNames(UnityEngine.Object[] assets, string[] assetNames, string pathName, BuildAssetBundleOptions assetBundleOptions)
        {
            BuildTarget webPlayer = BuildTarget.WebPlayer;
            return BuildAssetBundleExplicitAssetNames(assets, assetNames, pathName, assetBundleOptions, webPlayer);
        }

        public static bool BuildAssetBundleExplicitAssetNames(UnityEngine.Object[] assets, string[] assetNames, string pathName, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform)
        {
            try
            {
                return BuildAssetBundleInternal(null, assets, assetNames, pathName, assetBundleOptions, targetPlatform);
            }
            catch (System.Exception)
            {
                Debug.LogError("Internal Error in BuildPlayer.");
                EditorApplication.Exit(1);
                return false;
            }
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private static extern bool BuildAssetBundleInternal(UnityEngine.Object mainAsset, UnityEngine.Object[] assets, string[] assetNames, string pathName, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);
        public static string BuildPlayer(string[] levels, string locationPathName, BuildTarget target, BuildOptions options)
        {
            try
            {
                return BuildPlayerInternal(levels, locationPathName, target, options);
            }
            catch (System.Exception)
            {
                Debug.LogError("Internal Error in BuildPlayer.");
                EditorApplication.Exit(1);
                return string.Empty;
            }
        }

        private static string BuildPlayerInternal(string[] levels, string locationPathName, BuildTarget target, BuildOptions options)
        {
            if (InternalEditorUtility.HasPro())
            {
                return BuildPlayerInternalNoCheck(levels, locationPathName, target, options, false);
            }
            Debug.LogError("Building Player from editor scripts requires Unity PRO");
            return "Building Player from editor scripts requires Unity PRO";
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern string BuildPlayerInternalNoCheck(string[] levels, string locationPathName, BuildTarget target, BuildOptions options, bool delayToAfterScriptReload);
        public static string BuildStreamedSceneAssetBundle(string[] levels, string locationPath, BuildTarget target)
        {
            return BuildPlayer(levels, locationPath, target, BuildOptions.BuildAdditionalStreamedScenes);
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern string GetBuildTargetAdvancedLicenseName(BuildTarget target);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern BuildTargetGroup GetBuildTargetGroup(BuildTarget platform);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern string GetBuildTargetGroupDisplayName(BuildTargetGroup targetPlatformGroup);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern string GetBuildTargetGroupName(BuildTarget target);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern string GetBuildToolsDirectory(BuildTarget target);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern string GetMonoBinDirectory(BuildTarget target);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern string GetMonoLibDirectory(BuildTarget target);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern string GetMonoProfileLibDirectory(BuildTarget target, string profile);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern string GetPlaybackEngineDirectory(BuildTarget target, BuildOptions options);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern bool IsBuildTargetSupported(BuildTarget target);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern bool IsUnityScriptEvalSupported(BuildTarget target);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal static extern bool LicenseCheck(BuildTarget target);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern void PopAssetDependencies();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern void PushAssetDependencies();
    }
}

